﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BruteForceSerializer.Formatting;
using BruteForceSerializer.Normalization;
using NUnit.Framework;
using BruteForceSerializer.Contract.Internal;

namespace BruteForceSerializer.Tests
{
    [TestFixture]
    public class test_Formatter
    {
        [Test]
        public void Objekt_mit_Feld()
        {
            var no = new NormalizedObject("System.String", 0);
            no.AddField("f1", 42);

            var formattedObject = Formatter.Format(no, false);

            Assert.AreEqual(@"{""_Id"":0,""_Type"":""System.String"",""f1"":42}", formattedObject);

			Formatter.DeFormat(formattedObject);
        }

        [Test]
        public void Geschachtelte_Objekte()
        {
			var noParent = new NormalizedObject("System.String", 0);
            var noChild = new NormalizedObject("System.Int32", 1);
            noParent.AddField("child", noChild);

            var formattedObject = Formatter.Format(noParent, false);

			Assert.AreEqual(@"{""_Id"":0,""_Type"":""System.String"",""child"":{""_Id"":1,""_Type"":""System.Int32""}}", formattedObject);
			Formatter.DeFormat(formattedObject);
        }

        [Test]
        public void Objektreferenzen()
        {
			var noParent = new NormalizedObject("System.String", 0);
            var noRef = new NormalizedReference(99);
            noParent.AddField("someObject", noRef);

            var formattedObject = Formatter.Format(noParent, false);

			Assert.AreEqual(@"{""_Id"":0,""_Type"":""System.String"",""someObject"":{""_ObjectId"":99}}", formattedObject);

			Formatter.DeFormat(formattedObject);
        }

		[Test]
		public void ErrorReproduction()
		{
			string serialized = "{\"_Id\":2,\"_Type\":\"XcoAppSpaces.Contracts.WorkerResolve.ResolveResponse\"," +
								"\"ProcessorAttributes\":{\"_Id\":5,\"_Type\":\"System.Collections.Generic.Dictionary<System.Type,XcoAppSpaces.Core.XcoProcessorAttribute>\"," +
								"\"0\":{\"_Id\":6,\"_Type\":\"System.Collections.DictionaryEntry\",\"Key\":{\"_Id\":7,\"_Type\":\"System.RuntimeType\",\"fullname\":\"System.Int32\"},\"Value\":{\"_Id\":8,\"_Type\":\"XcoAppSpaces.Core.XcoExclusiveAttribute\",\"MessageTransferMode\":1}}," +
								"\"1\":{\"_Id\":9,\"_Type\":\"System.Collections.DictionaryEntry\",\"Key\":{\"_Id\":10,\"_Type\":\"System.RuntimeType\",\"fullname\":\"System.String\"},\"Value\":{\"_Id\":11,\"_Type\":\"XcoAppSpaces.Core.XcoConcurrentAttribute\",\"MessageTransferMode\":0}}," +
								"\"2\":{\"_Id\":12,\"_Type\":\"System.Collections.DictionaryEntry\",\"Key\":{\"_Id\":13,\"_Type\":\"System.RuntimeType\",\"fullname\":\"Microsoft.Ccr.Core.PortSet<System.Int32,System.String>\"},\"Value\":{\"_ObjectId\":11}}," +
								"\"3\":{\"_Id\":14,\"_Type\":\"System.Collections.DictionaryEntry\",\"Key\":{\"_Id\":15,\"_Type\":\"System.RuntimeType\",\"fullname\":\"System.Boolean\"},\"Value\":{\"_Id\":16,\"_Type\":\"XcoAppSpaces.Core.XcoTeardownAttribute\",\"MessageTransferMode\":0}}}}";
			var deFormatted = Formatter.DeFormat(serialized);
			Assert.AreEqual(1, deFormatted.Fields.Count());
			Assert.AreEqual(4, ((INormalizedObject)deFormatted.Fields.First().Value).Fields.Count());
		}
    }
}
